package com.itheima.springbootwebtest.service.impl;

import com.itheima.springbootwebtest.exception.CommonExceptionHander;
import com.itheima.springbootwebtest.mapper.EmpMapper;
import com.itheima.springbootwebtest.pojo.Emp;
import com.itheima.springbootwebtest.pojo.EmpLoginDTO;
import com.itheima.springbootwebtest.pojo.LoginVO;
import com.itheima.springbootwebtest.service.EmpService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

/**
 * TODO
 *
 * @Author mr.luo
 * @Date 14 4月 2025 11:22
 */
@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;
    /**
     * 登录
     *
     * @return
     */
    @Override
    public LoginVO login(EmpLoginDTO empLoginDTO) {
        //校验用户名与密码是否存在
        Emp emp = empMapper.login(empLoginDTO.getUsername());
        if (emp == null) {
            throw new CommonExceptionHander("用户账号错误！！！");
        }
        //校验账户密码
        String password = empLoginDTO.getPassword();
        //使用MD5加密
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(emp.getPassword())) {
            throw new CommonExceptionHander("用户密码错误！！！");
        }
        //把emp拷贝给loginVO
        LoginVO loginVO = new LoginVO();
        BeanUtils.copyProperties(emp, loginVO);
        return loginVO;
    }


}
